package com.xoms.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xoms.admin.controller.res.bean.RProviderBean;
import com.xoms.admin.model.AccountBean;
import com.xoms.admin.model.ProviderBean;
import com.xoms.admin.persist.dao.ProviderDao;
import com.xoms.admin.service.ProviderService;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.DateTimeUtil;

@Service
public class ProviderServiceImpl implements ProviderService {
	
	@Autowired
	ProviderDao providerImpl;

	@Override
	public PageResult<RProviderBean> getProvider(AccountBean account, String key, int page, int pagesize) {
		PageResult<RProviderBean> result = new PageResult<RProviderBean>();
		List<RProviderBean> resList = new ArrayList<RProviderBean>();
		//查询供应商数量
		int count=providerImpl.selectCount(key);
		//查询供应商列表
		if(count>0) {
			List<ProviderBean> lists = providerImpl.selectList(key, page, pagesize);
			
			if (lists != null) {
				RProviderBean rb = null;
				for (ProviderBean acc : lists) {
					rb=new RProviderBean();
					rb=parse(acc);
					resList.add(rb);
				}
			}
		}
		result.setData(resList);
		result.setCount(count);
		return result;
	}

	@Override
	public boolean del(AccountBean account, int pid) {
		boolean flag = false;
		ProviderBean pro = providerImpl.selectOneById(pid);
		if(pro == null) {
			throw new ServiceException("object_is_not_exist","供应商");
		}
		flag = providerImpl.del(pid) > 0;
		if(!flag) {
			throw new ServiceException("database_delete_fail","供应商");
		}
		return flag;
	}



	@Override
	public RProviderBean edit(AccountBean account, int pid, String theatreId, String name,String linkMan, String phoneNum,
			String addr, String remark, int status) {
		boolean flag = false;
		ProviderBean data=new ProviderBean();
		data.setTheatreId(theatreId);
		data.setName(name);
		data.setLinkMan(linkMan);
		data.setPhoneNum(phoneNum);
		data.setAddr(addr);
		data.setRemark(remark);
		data.setTheatreId(theatreId);
		data.setStatus(status);
		
		if(pid != 0) {// 有  修改
			data.setPid(pid);
			data.setStatus(1);
			flag = providerImpl.update(data) > 0;
			if(!flag) {
				throw new ServiceException("database_update_fail","供应商");
			}
		}else {//添加
			data.setAccountId(account.getAccid());
			flag = providerImpl.insert(data) > 0;
			if(!flag) {
				throw new ServiceException("database_insert_fail","供应商");
			}
		}
		return parse(providerImpl.selectOneById(data.getPid()));
	}


	
	/**
	 * 格式化数据
 	 * @param punit
 	 * @return
 	 */
 		private RProviderBean parse(ProviderBean provider) {
 			RProviderBean res = new RProviderBean();
 			res.setPid(provider.getPid());
 			res.setTheatre(provider.getTheatre());
 			res.setAccount(provider.getAccount());
 			res.setTheatreId(provider.getTheatreId());
 			res.setName(provider.getName());
 			res.setLinkMan(provider.getLinkMan());
 			res.setPhoneNum(provider.getPhoneNum());
 			res.setAddr(provider.getAddr());
 			res.setTotalCost(provider.getTotalCost());
 			res.setRemark(provider.getRemark());
 			res.setStatus(provider.getStatus());
 			res.setCreateTime(DateTimeUtil.formatTimestamp(provider.getCreateTime()));
 			return res;
 		}
	

}
